package com.xywy.wjk;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.app.MainApplication;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;
import com.xywy.wjk.adapter.AddDataAdapter;
import com.xywy.wjk.bean.AddData;

/**
 * @ClassName: AddActivity
 * @Description: 添加设备
 * 
 * @author ZhiluCui
 * @date 2014-10-21 上午10:53:40
 * @version:1.0
 */
public class AddDevicesActivity extends BaseActivity implements
		View.OnClickListener {
	private static final String TAG = "AddDevicesActivity";
	TitleBar2 titleBar;
	TextView tvBack;
	TextView tvGo;
	private Context mContext;
	String fragTarget = "";
	String content = "";
	List<AddData> list = new ArrayList<AddData>();
	private ListView lv;
	private AddDataAdapter adapter;
	private RelativeLayout rlDeviceTypeAdd;
	private RelativeLayout rlRadarScanAdd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.wjk_add);

		this.preInit();
		this.initView();
		this.loadData();
		this.setListener();
		MainApplication.addSpeciall(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onEvent(mContext, "A001");

	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	public void preInit() {
		mContext = this.context;
		fragTarget = getIntent().getStringExtra("fragTarget");
		SharedPreferences sp = context.getSharedPreferences("config",
				Context.MODE_PRIVATE);
//		String userId = sp.getString("userid", "");
		String userId = UserKeeper.getCurrentUser(this).getUserid();
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + userId);
	}

	@Override
	public void initView() {
		// title
		titleBar = (TitleBar2) findViewById(R.id.wjk_add_title_bar);

		TextView tvTitle = titleBar.getTitle();
		tvBack = titleBar.getTvBack();
		tvGo = titleBar.getGo();
		tvGo.setVisibility(View.GONE);
		// tvGo.setText(getString(R.string.common_confirm));
		// tvTitle.setText(getString(R.string.main_wjk_add));
		String tv_device = getIntent().getStringExtra("deviceType");
		tvTitle.setText(tv_device);

		tvBack.setText(getString(R.string.tv_start_back));

		rlDeviceTypeAdd = (RelativeLayout) this
				.findViewById(R.id.rl_device_type_add);
		rlDeviceTypeAdd.setOnClickListener(this);
		rlRadarScanAdd = (RelativeLayout) this
				.findViewById(R.id.rl_radar_scan_add);
		rlRadarScanAdd.setOnClickListener(this);
		// lv = (ListView) this.findViewById(R.id.lv_wjk_add);
		// lv.setDividerHeight(0);
	}

	@Override
	public void loadData() {

		list.add(new AddData(0, this.getString(R.string.main_wjk_add_bmi),
				false));
		list.add(new AddData(1, this.getString(R.string.main_wjk_add_tzl), true));
		list.add(new AddData(2, this.getString(R.string.main_wjk_add_ggzl),
				false));
		list.add(new AddData(3, this.getString(R.string.main_wjk_add_shl),
				false));
		adapter = new AddDataAdapter(this);
		adapter.setData(list);
		// lv.setAdapter(adapter);

		// lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		// @Override
		// public void onItemClick(AdapterView<?> adapterView, View view,
		// int position, long id) {
		// AddData addData = list.get(position);
		// MLog.i(TAG,
		// "ONiTEMcLICK==========点击后的数据====" + addData.getTitle()
		// + "  是否选中:" + addData.isChecked());
		//
		// ImageView check = (ImageView) view
		// .findViewById(R.id.iv_wjk_detail_add_item);
		//
		// MLog.i(TAG,
		// "ONiTEMcLICK=======删除后===点击后的数据===="
		// + addData.getTitle() + "  是否选中:"
		// + addData.isChecked());
		//
		// if (addData.isChecked()) {
		// addData.setChecked(false);
		// check.setBackgroundDrawable(mContext.getResources()
		// .getDrawable(R.drawable.wjk_add_dian));
		// } else {
		// addData.setChecked(true);
		// check.setBackgroundDrawable(mContext.getResources()
		// .getDrawable(R.drawable.wjk_add_dian_pressed));
		// }
		// list.add(addData);
		// }
		// });

	}

	public void setListener() {
		titleBar.onClickBack(AddDevicesActivity.this);
		// tvGo.setOnClickListener(this);
		tvBack.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_title_back:
			// back();
			finish();
			break;

		case R.id.rl_device_type_add:
			Intent intent = new Intent(this, DeviceTypeAddActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_radar_scan_add:
			Intent intentScan = new Intent(AddDevicesActivity.this,
					ScanBLEActivity.class);
			startActivity(intentScan);

			break;
		// case R.id.tv_title_go:
		// go();
		// break;
		}
	}

	private void go() {

		for (int i = 0; i < list.size(); i++) {
			AddData data = list.get(i);
			if (data.isChecked()) {
				content += data.getTitle() + ",";
			}
		}
		content = content.substring(0, content.length() - 1);
		MLog.i(TAG, "选中的文本:" + content);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(TAG, "AddDevicesActivity onDestroy");
	}

}
